﻿using HCM_maps_website.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HCM_maps_website.Process;

namespace HCM_maps_website.Controllers
{
    public class AdminController : Controller
    {
        public static int NUM_RECORD_PER_PAGE = 15;
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            return RedirectToAction("Address");
        }

        public ActionResult Login()
        {
            if (Session["Admin"] != null) return RedirectToAction("Index");
            return View();
        }

        [HttpPost]
        public ActionResult Login(string username,string password)
        {
            if (username.ToLower().Equals("admin") && password.Equals("123"))
            {
                Session["Admin"] = username;
                return RedirectToAction("Index");
            }

            return View();
        }

        #region Address
        public ActionResult Address(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();


            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<Address> results = dbContext.Addresses.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View("Index",new Tuple<IEnumerable<Address>,int>(results,dbContext.Addresses.Count()));
        }

        public ActionResult InsertAddress(string number,string area,float xlocation, float ylocation) {

            Address addr = new Process.Address()
            {
                Number = number,
                Area = area,
                xLocation = xlocation,
                yLocation = ylocation
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newAddr = dbContext.Addresses.FirstOrDefault(x => x.ID == addr.ID);
            if (newAddr == null)
            {
                dbContext.Addresses.InsertOnSubmit(addr);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("Address");
        }

        public ActionResult EditAddress(int? id, string number, string area, float xlocation, float ylocation, string editadd)
        {
            if (editadd == "Update")
            {
                UpdateAddress(id, number, area, xlocation, ylocation);
            }
            else
            {
                DeleteAddress(id);
            }
            return RedirectToAction("Address");
        }

        public void UpdateAddress(int? id, string number, string area, float xlocation, float ylocation)
        {

            Address addr = new Process.Address()
            {
                ID = id.Value,
                Number = number,
                Area = area,
                xLocation = xlocation,
                yLocation = ylocation
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Addresses.FirstOrDefault(x => x.ID == addr.ID);
            // 2. update
            if (update != null)
            {
                update.Number = addr.Number;
                update.xLocation = addr.xLocation;
                update.yLocation = addr.yLocation;
                update.Area = addr.Area;
                
                dbContext.SubmitChanges();
            }
        }

        public void DeleteAddress(int? id) {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Addresses.FirstOrDefault(x => x.ID == id);
            if (update != null)
            {
                dbContext.Addresses.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion

        #region NodeInfo
        public ActionResult NodeInfo(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();
            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<HCM_maps_website.Process.NodeInfo> results = dbContext.NodeInfos.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View(new Tuple<IEnumerable<HCM_maps_website.Process.NodeInfo>, int>(results, dbContext.NodeInfos.Count()));
        }

        public ActionResult InsertNodeInfo(float xlocation, float ylocation)
        {

            var info = new Process.NodeInfo()
            {
                xLocation = xlocation,
                yLocation = ylocation
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newinfo = dbContext.NodeInfos.FirstOrDefault(x => x.ID == info.ID);
            if (newinfo == null)
            {
                dbContext.NodeInfos.InsertOnSubmit(info);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("NodeInfo");
        }

        public ActionResult EditNodeInfo(int? id, float xlocation, float ylocation, string editadd)
        {
            if (editadd == "Update")
            {
                UpdateNodeInfo(id, xlocation, ylocation);
            }
            else
            {
                DeleteNodeInfo(id);
            }
            return RedirectToAction("NodeInfo");
        }

        public void UpdateNodeInfo(int? id, float xlocation, float ylocation)
        {

            var info = new Process.NodeInfo()
            {
                xLocation = xlocation,
                yLocation = ylocation
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.NodeInfos.FirstOrDefault(x => x.ID == info.ID);
            // 2. update
            if (update != null)
            {
                update.xLocation = info.xLocation;
                update.yLocation = info.yLocation;

                dbContext.SubmitChanges();
            }
        }

        public void DeleteNodeInfo(int? id)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.NodeInfos.FirstOrDefault(x => x.ID == id);
            if (update != null)
            {
                dbContext.NodeInfos.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion

        #region Place
        public ActionResult Place(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();
            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<HCM_maps_website.Process.Place> results = dbContext.Places.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View(new Tuple<IEnumerable<HCM_maps_website.Process.Place>, int>(results, dbContext.Places.Count()));
        }

        public ActionResult InsertPlace(int type, string fullName, string addr, float xlocation, float ylocation, string info, string picture, string phone, string website)
        {
            var place = new Process.Place { 
                Type = type,
                FullName = fullName,
                Address = addr,
                xLocation = xlocation,
                yLocation = ylocation,
                Info = info,
                Picture = picture,
                Phone = phone,
                Website = website
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newplace = dbContext.NodeInfos.FirstOrDefault(x => x.ID == place.ID);
            if (newplace == null)
            {
                dbContext.Places.InsertOnSubmit(place);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("Place");
        }

        public ActionResult EditPlace(int? id, int type, string fullName, string addr, float xlocation, float ylocation, string info, string picture, string phone, string website, string editadd)
        {
            if (editadd == "Update")
            {
                UpdatePlace(id, type, fullName, addr, xlocation, ylocation, info,  picture,  phone,  website);
            }
            else
            {
                DeletePlace(id);
            }
            return RedirectToAction("Place");
        }

        public void UpdatePlace(int? id, int type, string fullName, string addr, float xLocation, float yLocation, string info, string picture, string phone, string website)
        {
            var place = new Process.Place {
                ID = id.Value,
                Type = type,
                FullName = fullName,
                Address = addr,
                xLocation = xLocation,
                yLocation = yLocation,
                Info = info,
                Picture = picture,
                Phone = phone,
                Website = website
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Places.FirstOrDefault(x => x.ID == place.ID);
            // 2. update
            if (update != null)
            {
                update.xLocation = place.xLocation;
                update.yLocation = place.yLocation;

                dbContext.SubmitChanges();
            }
        }

        public void DeletePlace(int? id)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Places.FirstOrDefault(x => x.ID == id);
            if (update != null)
            {
                dbContext.Places.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion

        #region PlaceType
        public ActionResult PlaceType(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();
            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<HCM_maps_website.Process.PlaceType> results = dbContext.PlaceTypes.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View(new Tuple<IEnumerable<HCM_maps_website.Process.PlaceType>, int>(results, dbContext.PlaceTypes.Count()));
        }

        public ActionResult InsertPlaceType(string description, string picture)
        {
            var type = new Process.PlaceType { 
                description = description,
                picture = picture
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newplace = dbContext.PlaceTypes.FirstOrDefault(x => x.id == type.id);
            if (newplace == null)
            {
                dbContext.PlaceTypes.InsertOnSubmit(type);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("PlaceType");
        }

        public ActionResult EditPlaceType(int? id, string description, string picture, string editadd)
        {
            if (editadd == "Update")
            {
                UpdatePlaceType(id, description, picture);
            }
            else
            {
                DeletePlaceType(id);
            }
            return RedirectToAction("PlaceType");
        }

        public void UpdatePlaceType(int? id, string description, string picture)
        {
            var type = new Process.PlaceType
            {
                description = description,
                picture = picture
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.PlaceTypes.FirstOrDefault(x => x.id == type.id);
            // 2. update
            if (update != null)
            {
                update.description = type.description;
                update.picture = type.picture;

                dbContext.SubmitChanges();
            }
        }

        public void DeletePlaceType(int? id)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.PlaceTypes.FirstOrDefault(x => x.id == id);
            if (update != null)
            {
                dbContext.PlaceTypes.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion

        #region Street
        public ActionResult Street(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();
            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<Process.Street> results = dbContext.Streets.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View(new Tuple<IEnumerable<Street>, int>(results, dbContext.Streets.Count()));
        }

        public ActionResult InsertStreet(string fullName, int beginPoint, int endPoint, int totalSegment)
        {
            var street = new Process.Street { 
                FullName = fullName,
                BeginPoint = beginPoint,
                EndPoint = endPoint,
                TotalSegment = totalSegment
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newAddr = dbContext.Streets.FirstOrDefault(x => x.ID == street.ID);
            if (newAddr == null)
            {
                dbContext.Streets.InsertOnSubmit(street);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("Street");
        }

        public ActionResult EditStreet(int? id, string fullName, int beginPoint, int endPoint, int totalSegment, string editadd)
        {
            if (editadd == "Update")
            {
                UpdateStreet(id, fullName, beginPoint, endPoint, totalSegment);
            }
            else
            {
                DeleteStreet(id);
            }
            return RedirectToAction("Street");
        }

        public void UpdateStreet(int? id, string fullName, int beginPoint, int endPoint, int totalSegment)
        {
            var street = new Process.Street
            {
                FullName = fullName,
                BeginPoint = beginPoint,
                EndPoint = endPoint,
                TotalSegment = totalSegment
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Streets.FirstOrDefault(x => x.ID == street.ID);
            // 2. update
            if (update != null)
            {
                update.FullName = fullName;
                update.BeginPoint = beginPoint;
                update.EndPoint = endPoint;
                update.TotalSegment = totalSegment;

                dbContext.SubmitChanges();
            }
        }

        public void DeleteStreet(int? id)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Streets.FirstOrDefault(x => x.ID == id);
            if (update != null)
            {
                dbContext.Streets.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion

        #region StreetSegment
        public ActionResult StreetSegment(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();
            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<StreetSegment> results = dbContext.StreetSegments.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View(new Tuple<IEnumerable<StreetSegment>, int>(results, dbContext.StreetSegments.Count()));
        }

        public ActionResult InsertStreetSegment(int startPoint, int endPoint, int twoWay, int idStreet, int order)
        {
            var segment = new Process.StreetSegment { 
                StartPoint = startPoint,
                EndPoint = endPoint,
                TwoWay = twoWay,
                IDStreet = idStreet,
                Order = order
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newSegment = dbContext.StreetSegments.FirstOrDefault(x => x.StartPoint == segment.StartPoint && x.EndPoint == segment.EndPoint);
            if (newSegment == null)
            {
                dbContext.StreetSegments.InsertOnSubmit(segment);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("StreetSegment");
        }

        public ActionResult EditStreetSegment(int startPoint, int endPoint, int twoWay, int idStreet, int order, string editadd)
        {
            if (editadd == "Update")
            {
                UpdateStreetSegment(startPoint, endPoint, twoWay, idStreet, order);
            }
            else
            {
                DeleteStreetSegment(startPoint, endPoint);
            }
            return RedirectToAction("StreetSegment");
        }

        public void UpdateStreetSegment(int startPoint, int endPoint, int twoWay, int idStreet, int order)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.StreetSegments.FirstOrDefault(x => x.StartPoint == startPoint && x.EndPoint == endPoint);
            // 2. update
            if (update != null)
            {
                update.StartPoint = startPoint;
                update.EndPoint = endPoint;
                update.TwoWay = twoWay;
                update.IDStreet = idStreet;
                update.Order = order;

                dbContext.SubmitChanges();
            }
        }

        public void DeleteStreetSegment(int startPoint, int endPoint)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.StreetSegments.FirstOrDefault(x => x.StartPoint == startPoint && x.EndPoint == endPoint);
            if (update != null)
            {
                dbContext.StreetSegments.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion

        #region ImageLocation
        public ActionResult ImageLocation(int? ipage)
        {
            if (Session["Admin"] == null) return RedirectToAction("Login");

            MapDataDataContext dbContext = new MapDataDataContext();
            int skip = ipage != null ? (ipage.Value - 1) * NUM_RECORD_PER_PAGE : 0;

            Session["page"] = ipage != null ? ipage.Value : 1;

            IEnumerable<Location> results = dbContext.Locations.ToList().Skip(skip).Take(NUM_RECORD_PER_PAGE);

            return View(new Tuple<IEnumerable<Location>, int>(results, dbContext.Locations.Count()));
        }

        public ActionResult InsertImageLocation(string id, string location)
        {
            var imgLocation = new Process.Location { 
                IDImage =  id,
                Location1 = location
            };

            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai chua?
            var newImg = dbContext.Locations.FirstOrDefault(x => x.IDImage == imgLocation.IDImage);
            if (newImg == null)
            {
                dbContext.Locations.InsertOnSubmit(imgLocation);
                dbContext.SubmitChanges();
            }
            return RedirectToAction("ImageLocation");
        }

        public ActionResult EditImageLocation(string id, string location, string editadd)
        {
            if (editadd == "Update")
            {
                UpdateImageLocation(id, location);
            }
            else
            {
                DeleteImageLocation(id);
            }
            return RedirectToAction("ImageLocation");
        }

        public void UpdateImageLocation(string id, string location)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Locations.FirstOrDefault(x => x.IDImage == id);
            // 2. update
            if (update != null)
            {
                update.IDImage = id;
                update.Location1 = location;

                dbContext.SubmitChanges();
            }
        }

        public void DeleteImageLocation(string id)
        {
            MapDataDataContext dbContext = new MapDataDataContext();
            // 1. kiem tra co ton tai trong db k?
            var update = dbContext.Locations.FirstOrDefault(x => x.IDImage == id);
            if (update != null)
            {
                dbContext.Locations.DeleteOnSubmit(update);
                dbContext.SubmitChanges();
            }
        }
        #endregion 
        public ActionResult Logout()
        {
            Session["Admin"] = null;

            return RedirectToAction("Login");
        }
    }
}
